﻿using Xejen.Communication.Configuration;
using Xejen.Communication.Endpoints;
using Xejen.Communication.Soap.Abstract;
using Xejen.Communication.Soap.Configuration;
using Xejen.Communication.Soap;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Xejen.Logger;
using Microsoft.Extensions.Logging;

namespace Xejen.Communication.Soap.Communicator
{
    /// <summary>
    /// 基于soap协议实现的通讯器
    /// </summary>
    /// <typeparam name="TSoapInvoker"><inheritdoc cref="ISoapInvoker{TSoapAddress}" path="/summary"/></typeparam>
    /// <creator>marc</creator>
    internal class SoapCommunicator<TSoapInvoker> : Communicator<SoapConfig, HeartbeatConfig, WcfAddress>, ICommunicator<SoapConfig, HeartbeatConfig>, ISoapCommunicator
        where TSoapInvoker : ISoapInvoker<WcfAddress>
    {
        private readonly ILoggerManager _loggerManager;
        /// <summary>
        /// 当通讯器停用时的消息提示
        /// </summary>
        private readonly string _alarmWhenCommunicatorDisabled = $"通讯器未启用，如若需要启用，请设置：{nameof(Enable)}属性为true.";

        private delegate Task<SoapResponse> SoapOperationAsync(WcfAddress address, int timeout, CancellationToken cancellationToken);
        private delegate Task<SoapResponse> SoapOperationAsync<TInput>(WcfAddress address, TInput input, int timeout, CancellationToken cancellationToken);

        /// <inheritdoc cref="ISoapInvokerCreator{TSoapInvoker}"/>
        protected ISoapInvokerCreator<TSoapInvoker> SoapInvokerCreator { get; }

        /// <inheritdoc/>
        protected override JsonSerializerOptions JsonSerializerOptions => Config.JsonSerializerOptions;

        /// <inheritdoc cref="SoapCommunicator{TSoapInvoker}"/>
        /// <param name="config"><inheritdoc cref="Communicator{TConfig, HeartbeatConfig, TAddress}.Config" path="/summary"/></param>
        /// <param name="soapInvokerCreator"><inheritdoc cref="SoapInvokerCreator" path="/summary"/></param>
        /// <param name="loggerManager"><inheritdoc cref="ILoggerManager" path="/summary"/></param>
        public SoapCommunicator(
            SoapConfig config,
            ISoapInvokerCreator<TSoapInvoker> soapInvokerCreator,
            ILoggerManager loggerManager) : base(config, loggerManager)
        {
            Check.NotNull(config, nameof(config));
            Check.NotNull(soapInvokerCreator, nameof(soapInvokerCreator));
            Check.NotNull(loggerManager, nameof(loggerManager));

            Config = config;
            SoapInvokerCreator = soapInvokerCreator;
            _loggerManager = loggerManager;
            Logger = loggerManager.CreateLogger<SoapCommunicator<TSoapInvoker>>();
        }

        /// <inheritdoc/>
        public virtual async Task<SoapResponse> InvokeServiceAsync(WcfAddress address, SoapConfig soapConfig = null, int timeout = -1, CancellationToken cancellationToken = default)
        {
            Check.NotNull(address, nameof(address));

            var config = soapConfig ?? Config;
            var task = SoapInvokerCreator.Create(config).InvokeServiceAsync(address, soapConfig, GetTimeout(timeout), cancellationToken);

            return await LogInvokeAsync(task, address, GetTimeout(timeout), cancellationToken);
        }

        /// <inheritdoc/>
        public virtual async Task<SoapResponse> InvokeServiceAsync<TInput>(WcfAddress address, TInput input, SoapConfig soapConfig=null, int timeout = -1, CancellationToken cancellationToken = default)
        {
            Check.NotNull(address, nameof(address));

            var config = soapConfig ?? Config;
            var task = SoapInvokerCreator.Create(config).InvokeServiceAsync(address, input, soapConfig, GetTimeout(timeout), cancellationToken);

            return await LogInvokeAsync(task, address, GetTimeout(timeout), input, cancellationToken);
        }
    }
}
